﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;
using System.Web.Security;

namespace ASV
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Connection mycon = new Connection();
            bool connectionOpen = mycon.connectDB();
            if (connectionOpen)
            {
                showUser(mycon);
            }
        }

        public void showUser(Connection dbConnection){
            object[] values = null;
            DataTable datatable = new DataTable();
            int userGroup;
            Authentication auth = new Authentication();

            datatable = dbConnection.select("SELECT * FROM `benutzer` LIMIT 0 , 30");

            for (int x = 0; x < datatable.Rows.Count; x++)
            {
                values = datatable.Rows[x].ItemArray;
            }

            /**
             * Elemente für Mitarbeiter und Administratoren werden ausgeblendet
             */
            ProductsAdmin.Visible = false;
            UserAdmin.Visible = false;
            /**************/

            if (auth.isLoggedIn() == false)
            {
                HeadLogin.Visible = true;
                HeadRegister.Visible = true;
                HeadLogout.Visible = false;
            }
            else
            {
                HeadLogin.Visible = false;
                HeadRegister.Visible = false;

                HeadLogout.Visible = true;

                Benutzer.Text = HttpContext.Current.Session["vorname"].ToString() + " " + HttpContext.Current.Session["nachname"].ToString();
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                if (auth.checkUserGroup(userGroup, 1))
                {
                    Downloads.Visible = true;
                    Support.Visible = true;
                    Benutzer.Visible = true;

                    if (auth.checkUserGroup(userGroup, 2) == true)
                    {
                        /**
                         * Bereich für die MITARBEITER wird sichbar gemacht
                         */
                        ProductsAdmin.Visible = true;

                        if (auth.checkUserGroup(userGroup, 3) == true)
                        {
                            /**
                             * Bereich für die ADMINISTRATOR wird sichbar gemacht
                             */
                            UserAdmin.Visible = true;
                        }
                    }
                }
            }
        }
    }
}
